﻿////////////////////////////////////////////////////////////////////////////////////////////////////
// file:	DBConnection\DBUpdate.cs
//
// summary:	Implements the database update class
////////////////////////////////////////////////////////////////////////////////////////////////////

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySql.Data.MySqlClient;
using Microsoft;

namespace Usermanagment.DBConnection
{
   ////////////////////////////////////////////////////////////////////////////////////////////////////
   /// <summary>
   ///  DataBase update class, responsible for updating data on existing users in the MYSQL Database.
   /// </summary>
   ///
   /// <remarks>	Avishai, 10/09/2012. </remarks>
   ////////////////////////////////////////////////////////////////////////////////////////////////////

   public class DBUpdate 
    {
        /// <summary>	The dbc. </summary>
        private DBConnect dbc;
        /// <summary>	The dbd. </summary>
        private DBDatafetch dbd;
		private string playersTable;
		private string statsTable;
	//	private Boolean disposed = false;

        ////////////////////////////////////////////////////////////////////////////////////////////////////
        /// <summary>	Initializes a new instance of the <see cref="DBUpdate"/> class. </summary>
        ///
        /// <remarks>	Avishai, 10/09/2012. </remarks>
        ///
        /// <param name="dbc">	The DBC. </param>
        ////////////////////////////////////////////////////////////////////////////////////////////////////

        public DBUpdate(DBConnect dbc)
        {
            this.dbc = dbc;
			this.playersTable = dbc.playerDB;
			this.statsTable = dbc.playerStats;
            dbd = new DBDatafetch(dbc);
        }

        ////////////////////////////////////////////////////////////////////////////////////////////////////
        /// <summary>	Updates the chips. </summary>
        ///
        /// <remarks>	Avishai, 10/09/2012. </remarks>
        ///
        /// <param name="username">	The username. </param>
        /// <param name="chips">   	The chips. </param>
        ///
        /// <returns>	true if it succeeds, false if it fails. </returns>
        ////////////////////////////////////////////////////////////////////////////////////////////////////

        public Boolean updateChips(String username, int chips)
        {
            int id = dbd.getID(username);
            if (id <= 0)
                return false;
            else
            {
				String command = "UPDATE `" + playersTable + "` SET Chips =" + chips + " WHERE ID =" + id;
                MySqlCommand cmd = new MySqlCommand(command,dbc.getCon());
                if (cmd.ExecuteNonQuery() > -1)
                    return true;
                else
                    return false;
            }
        }

        ////////////////////////////////////////////////////////////////////////////////////////////////////
        /// <summary>	Updates the canplay. </summary>
        ///
        /// <remarks>	Avishai, 10/09/2012. </remarks>
        ///
        /// <param name="username">	The username. </param>
        /// <param name="canp">	   	The canp. </param>
        ///
        /// <returns>	true if it succeeds, false if it fails. </returns>
        ////////////////////////////////////////////////////////////////////////////////////////////////////

        public Boolean updateCanplay(String username, int canp)
        {
            int id = dbd.getID(username);
            if (id <= 0)
                return false;
            else
            {
				String command = "UPDATE `" + playersTable + "` SET CanPlay ='" + canp + "' WHERE ID =" + id;
                MySqlCommand cmd = new MySqlCommand(command,dbc.getCon());
                if (cmd.ExecuteNonQuery() > -1)
                    return true;
                else
                    return false;
            }

        }

        ////////////////////////////////////////////////////////////////////////////////////////////////////
        /// <summary>	Updates the cancon. </summary>
        ///
        /// <remarks>	Avishai, 10/09/2012. </remarks>
        ///
        /// <param name="username">	The username. </param>
        /// <param name="canc">	   	The canc. </param>
        ///
        /// <returns>	true if it succeeds, false if it fails. </returns>
        ////////////////////////////////////////////////////////////////////////////////////////////////////

        public Boolean updateCancon(String username, int canc)
        {
            int id = dbd.getID(username);
            if (id <= 0)
                return false;
            else
            {
				String command = "UPDATE `" + playersTable + "` SET Canconnect ='" + canc + "' WHERE ID =" + id;
                MySqlCommand cmd = new MySqlCommand(command, dbc.getCon());
                if (cmd.ExecuteNonQuery() > -1)
                    return true;
                else
                    return false;
            }

        }

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Updates the wins. </summary>
		///
		/// <remarks>	Avishai, 10/09/2012. </remarks>
		///
		/// <param name="username">	The username. </param>
		/// <param name="wins">	   	The wins. </param>
		///
		/// <returns>	true if it succeeds, false if it fails. </returns>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		public Boolean updateWins(String username, int wins)
		{
			int t = dbd.getWins(username) + wins;
			String command = "UPDATE '" + statsTable + "' SET `Wins` = '" + t + "' WHERE `Username` = '" + username + "'";
			MySqlCommand cmd = new MySqlCommand(command, dbc.getCon());
			if (cmd.ExecuteNonQuery() > -1)
				return true;
			else
				return false;
		}

	    ////////////////////////////////////////////////////////////////////////////////////////////////////
	    /// <summary>	Updates the loss. </summary>
	    ///
	    /// <remarks>	Avishai, 10/09/2012. </remarks>
	    ///
	    /// <param name="username">	The username. </param>
	    /// <param name="loss">	   	The loss. </param>
	    ///
	    /// <returns>	true if it succeeds, false if it fails. </returns>
	    ////////////////////////////////////////////////////////////////////////////////////////////////////

	    public Boolean updateLoss(String username, int loss)
		{

			int t = dbd.getLoss(username) + loss;
			String command = "UPDATE '" + statsTable + "' SET `Loss` = '" + t + "' WHERE `Username` = '" + username + "'";
			MySqlCommand cmd = new MySqlCommand(command, dbc.getCon());
			if (cmd.ExecuteNonQuery() > -1)
				return true;
			else
				return false;
	    }

	    ////////////////////////////////////////////////////////////////////////////////////////////////////
	    /// <summary>	Updates the ratio described by username. </summary>
	    ///
	    /// <remarks>	Avishai, 10/09/2012. </remarks>
	    ///
	    /// <param name="username">	The username. </param>
	    ///
	    /// <returns>	true if it succeeds, false if it fails. </returns>
	    ////////////////////////////////////////////////////////////////////////////////////////////////////

	    public Boolean updateRatio(String username)
		{
			double d = dbd.getWins(username) / dbd.getLoss(username);
			String command = "UPDATE '" + statsTable + "' SET `Ratio` ='" + Convert.ToString(d) + "' WHERE `Username` ='" + username + "'";
			MySqlCommand cmd = new MySqlCommand(command, dbc.getCon());
			if (cmd.ExecuteNonQuery() > -1)
				return true;
			else
				return false;
		}

        ////////////////////////////////////////////////////////////////////////////////////////////////////
        /// <summary>	Deletes the user described by username. </summary>
        ///
        /// <remarks>	Avishai, 10/09/2012. </remarks>
        ///
        /// <param name="username">	The username. </param>
        ///
        /// <returns>	true if it succeeds, false if it fails. </returns>
        ////////////////////////////////////////////////////////////////////////////////////////////////////

        public Boolean deleteUser(String username)
        {
            int id = dbd.getID(username);
            if (id <= 0)
                return false;
            else
            {
				String command = "DELETE FROM `" + playersTable + "` WHERE ID =" + id;
                MySqlCommand cmd = new MySqlCommand(command, dbc.getCon());
				if (cmd.ExecuteNonQuery() > -1)
				{
					cmd.CommandText = "DELETE FROM '" + statsTable + "' WHERE `Username` = '" + username + "'";
					cmd.ExecuteNonQuery();
					return true;
				}
				else
					return false;
            }
        }


	   
	}
}
